package com.example.gybicycle.service;

import java.util.Date;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.sql.CrudService;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.dao.LockDao;
import com.example.gybicycle.entity.Bike;
import com.example.gybicycle.dao.BikeDao;
import com.example.gybicycle.entity.Lock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BikeService extends CrudService<BikeDao, Bike> {
    @Autowired
    private FileService fileService;
    @Autowired
    private LockDao lockDao;
    @Override
    public Bike get(String id) {
        Bike table=super.get(id);
        return table;
    }
    public Result save(Bike entity,String loginId){
        int num=0;
        if(entity.getId()==null){
           
            num=dao.insert(entity);
        }else {
           
            num=dao.update(entity);
        }
        if(num>0)
            return Result.success(entity);
        else 
            return Result.fail("保存失败");
    }

    @Override
    public int insert(Bike entity) {
        entity.setId(NumberUtil.getNum(NumType.BIKE));
        String qrcode=fileService.createBikeQRCode(entity);
        entity.setQrcode(qrcode);
        int num=super.insert(entity);
        return  num;
    }

    @Override
    public int update(Bike entity) {
        entity.setEditTime(new Date());
//        String qrcode=fileService.createBikeQRCode(entity);
//        entity.setQrcode(qrcode);
        return super.update(entity);
    }

    @Override
    public int insertByList(List<Bike> list) {
        for(Bike entity:list){
            entity.setId(NumberUtil.getNum(NumType.BIKE));
            String qrcode=fileService.createBikeQRCode(entity);
            entity.setQrcode(qrcode);
        }
        return super.insertByList(list);
    }
    public int  openBike(Bike bike){
        Bike bike1=new Bike();
        bike1.setBikeState(1);
        bike1.setLockId("");
        int num=dao.update(bike);
        Lock lock=new Lock();
        lock.setId(bike.getLockId());

        return num;
    }
    public List<Bike> findList(Bike entity){
        List<Bike> list=super.findList(entity);
        return list;
    }
    public Bike getByLock(String id){
        return dao.getByLock(id);
    }


    public Bike getByLatchNo(String id){
        return dao.getByLatchNo(id);
    }
    public List<String> findCode(Bike bike){
        return  dao.findCode(bike);
    }


}